var csrf = $("meta[property=_csrf]").attr("content");
$.ajaxSetup({
    global: true,
    headers: {
        'X-CSRF-TOKEN': csrf
    }
});
var socket;
$(document).ready(function () {
    // handle menu
    handleMenuActive();
    if(typeof(WebSocket) === "undefined") {
        console.error("您的浏览器不支持WebSocket……")
    } else {
        socket = new WebSocket("ws://localhost:8080/push");
        socket.onopen = function () {
            alert("连接成功");
        }
        socket.onmessage = function (msg) {
            alert(msg);
        }
        socket.onerror = function() {
            alert("发生了错误");
        }
    }
});

/**
 * 页面加载之后自动展开菜单
 */
function handleMenuActive() {
    var url = location.pathname.toLowerCase();
    var el = null;
    var menu = $("#side-menu");
    menu.find('li > a').each(function () {
        var path = $(this).attr('href');
        if (path) {
            path = path.toLowerCase();
            if (path === url) {
                el = $(this);
                return
            }
        }
    });

    if (!el) {
        return;
    }

    if (el.attr('href') == 'javascript:;' || el.attr('href') == '#') {
        return;
    }

    el.parents('li').each(function () {
        $(this).addClass('active');
        $(this).parent('ul.nav').addClass('in');
    });

}